#pragma once

#include <vector>
#include <string>

#include <rtc_base/logging.h>
#include <rtc_base/third_party/sigslot/sigslot.h>

#include "base/event_loop.h"
#include "ice_def.h"
#include "ice/port_allocator.h"
#include "ice/ice_credentials.h"
#include "ice/candidate.h"
#include "ice/stun.h"

namespace xrtc {

class UDPPort;

class IceTransportChannel : public sigslot::has_slots<> {
public:
    IceTransportChannel(EventLoop* el, PortAllocator* allocator, 
                        const std::string& transport_name, IceCandidateComponent component);
    
    virtual ~IceTransportChannel() = default;

    void setIceParameters(const IceParameters& ice_parameters);

    void set_remote_ice_params(const IceParameters& ice_parameters);

    std::string getTransportName() const { return m_transport_name; }
    IceCandidateComponent getComponent() const { return m_component; }  

    // 收集candidate (一个传输地址，由ip、port、protocol构成)
    void gathering_candidate();

    std::string to_string();

public:
    // Candidate收集完成之后发送的信号
    sigslot::signal2<IceTransportChannel*, const std::vector<Candidate>&> signal_candidate_allocate_done;    
    
private:
    // 处理未知地址（客户端还未与服务器交换过Candidate，客户端NAT映射的反射地址）信号, 创建反射地址的Candidate
    void _on_unknown_address(UDPPort* port, const rtc::SocketAddress& addr, StunMessage* msg, const std::string& remote_ufrag);


private: 
    EventLoop* m_el;
    std::string m_transport_name;      // trransposrt_name : audio/video
    IceCandidateComponent m_component; // component: rtp/rtcp 
    PortAllocator* m_allocator;
    IceParameters m_ice_parameters;
    IceParameters m_remote_ice_parameters;
    std::vector<Candidate> m_local_candidates;
};    

}